package com.ruoyi.web.controller.finance;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.finance.domain.BatchBilling;
import com.ruoyi.finance.service.IBatchBillingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 分批计费(应收)Controller
 * 
 * @author liuyu
 * @date 2024-10-30
 */
@RestController
@RequestMapping("/finance/billing")
public class BatchBillingController extends BaseController
{
    @Autowired
    private IBatchBillingService batchBillingService;


    /**
     * 获取分批计费(应收)详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:billing:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(batchBillingService.selectBatchBillingById(id));
    }

    /**
     * 新增分批计费(应收)
     */
    @PreAuthorize("@ss.hasPermi('finance:billing:add')")
    @Log(title = "分批计费(应收)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BatchBilling batchBilling)
    {
        return toAjax(batchBillingService.insertBatchBilling(batchBilling));
    }

    /**
     * 修改分批计费(应收)
     */
    @PreAuthorize("@ss.hasPermi('finance:billing:edit')")
    @Log(title = "分批计费(应收)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BatchBilling batchBilling)
    {
        return toAjax(batchBillingService.updateBatchBilling(batchBilling));
    }


    /**
     * 获取收款总额
     * @param accountsReceivableId 关联应付表id
     * @return
     */
    @GetMapping("/accountsReceivableId")
    public AjaxResult getSumAmount(@RequestParam("accountsReceivableId") Long accountsReceivableId){
        return success(batchBillingService.selectSumAmount(accountsReceivableId));
    }

    /**
     * 根据id获取计费记录
     * @param id
     * @return
     */
    @GetMapping("byId")
    public AjaxResult getBillingById(Long id){
        return success(batchBillingService.getBillingById(id));
    }

    /**
     * 上传附件图片   上传到本地
     * @param other
     * @return
     * @throws IOException
     * @throws InvalidExtensionException
     */
    @PostMapping("other")
    public AjaxResult other(@RequestParam("other") MultipartFile other) throws IOException, InvalidExtensionException {
        String upload = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), other, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        upload = "localhost:8080"  + upload;
        return success(upload);
    }
}
